<template>
    <div>
        <table class="table">
            <thead>
                <tr>
                    <td>Name</td>
                    <td>
                        <input v-model="obj.name" type="text">
                    </td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>
                        <input v-model="obj.password" type="password" >
                    </td>
                </tr>
                <tr>
                    <td>REPassword</td>
                    <td>
                        <input v-model="obj.rePassword" type="password" >
                    </td>
                </tr>
                <tr>
                    <td>Img</td>
                    <td>
                        <input type="file" @change="FileChange">
                        <img :src="'https://localhost:7124'+obj.img" style="height: 120px; height: 130px;">
                    </td>
                </tr>
                <tr>
                    <td>dateTime</td>
                    <td>
                        <input v-model="obj.dateTime" type="date" >
                    </td>
                </tr>
                <tr>
                    <td>Sex</td>
                    <td>
                        <input v-model="obj.sex" type="radio" name="sex" :value="1">男
                        <input v-model="obj.sex" type="radio" name="sex" :value="2">女
                    </td>
                </tr>
                <tr>
                    <td>State</td>
                    <td>
                        <input v-model="obj.state" type="checkbox">选中为开启
                    </td>
                </tr>
                <tr>
                    <td>Hobby</td>
                    <td>
                        <input v-model="obj.HobbyArray" type="checkbox" value="登山">登山
                        <input v-model="obj.HobbyArray" type="checkbox" value="跳舞">跳舞
                        <input v-model="obj.HobbyArray" type="checkbox" value="打篮球">打篮球
                        <input v-model="obj.HobbyArray" type="checkbox" value="打太极">打太极
                    </td>
                </tr>
                <tr>
                    <td>LevelId</td>
                    <td>
                        <select  v-model="obj.levelId">
                            <option value="0">请选择</option>
                            <option value="1">金牌</option>
                            <option value="2">银牌</option>
                            <option value="3">铜牌</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="添加" @click="add" class="btn">
                    </td>
                </tr>
            </thead>
        </table>
    </div>
</template>

<script setup lang="ts">
import router from '@/router';
import axios from 'axios';
import { ref } from 'vue';

const obj=ref({
    "id": 0,
    "name": "",
    "password": "",
    "rePassword": "",
    "img": "",
    "dateTime": "2025-02-14T11:25:53.992Z",
    "sex": 0,
    "state": true,
    "hobby": "",
    HobbyArray:[],
    "levelId": 0
})
const add=()=>{
    axios.post("https://localhost:7124/api/ZuoYeInfo/Add",obj.value).then(res=>{
        if(res.data>0){
            alert('添加成功')
            router.push("/Show");
        }
        else{
            alert('添加失败')
        }
    })
}

const FileChange=(e:any)=>{
    var f = e.target.files[0];
    var fd = new FormData();
    fd.append("file", f);

    if(f.size>1024*1024*2){
        alert('图片最大只能是2MB');
        return;
    }

    if(f.type=='image/jpeg'||f.type=='image/png'){
        axios.post("https://localhost:7124/api/ZuoYeInfo/UpFile",fd).then(res=>{
        obj.value.img=res.data;
     })
    }
    else{
        alert('只能上传jpeg||png格式')
    }
   
}

</script>

<style scoped>

</style>